package net.zhuike.novel.app.ui.adapter;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;

import net.zhuike.novel.app.R;
import net.zhuike.novel.app.bean.RankingList;
import net.zhuike.novel.app.bean.TabModel;
import net.zhuike.novel.app.common.OnRvItemClickListener;
import net.zhuike.novel.app.ui.base.Constant;

import java.util.List;

/**
 * @author wq
 * @date 17/6/13.
 */
public class TopRankAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private LayoutInflater inflater;

    private List<TabModel> groupArray;
    private List<List<TabModel>> childArray;

    private OnRvItemClickListener<TabModel> listener;

    public TopRankAdapter(Context context, List<TabModel> groupArray, List<List<TabModel>> childArray) {
        this.childArray = childArray;
        this.groupArray = groupArray;
        mContext = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getGroupCount() {
        return groupArray.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childArray.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupArray.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childArray.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        final View group = inflater.inflate(R.layout.item_top_rank_group, null);

        ImageView ivCover = (ImageView) group.findViewById(R.id.ivRankCover);
        if (!TextUtils.isEmpty(groupArray.get(groupPosition).getIcon())) {
            Glide.with(mContext).load(groupArray.get(groupPosition).getIcon()).placeholder(R.mipmap.avatar_default)
                    .into(ivCover);

        } else {
            ivCover.setImageResource(R.mipmap.ic_rank_collapse);
        }
        group.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(group, groupPosition, groupArray.get(groupPosition));
                }
            }
        });
        TextView tvName = (TextView) group.findViewById(R.id.tvRankGroupName);
        tvName.setText(groupArray.get(groupPosition).getName());

        ImageView ivArrow = (ImageView) group.findViewById(R.id.ivRankArrow);
        ivArrow.setVisibility(View.GONE);
        return group;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final View child = inflater.inflate(R.layout.item_top_rank_child, null);

        TextView tvName = (TextView) child.findViewById(R.id.tvRankChildName);
        tvName.setText(childArray.get(groupPosition).get(childPosition).getName());

        child.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClick(child, childPosition, childArray.get(groupPosition).get(childPosition));
            }
        });
        return child;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public void setItemClickListener(OnRvItemClickListener<TabModel> listener) {
        this.listener = listener;
    }
}
